Skip to content

Report overflowing durations as invalid instead of crashing - #1550

Closed
aryansk wants to merge 1 commit into
python-jsonschema:mainfrom
aryansk:fix-duration-overflow-1511
Closed

Report overflowing durations as invalid instead of crashing#1550
aryansk wants to merge 1 commit into
python-jsonschema:mainfrom
aryansk:fix-duration-overflow-1511

Conversation

@aryansk

@aryansk aryansk commented Aug 15, 2026

Copy link
Copy Markdown

What

Fixes #1511 — the duration format checker no longer crashes with an uncaught decimal.Overflow on strings whose amounts exceed Decimal's context limits; they are reported as invalid durations like any other malformed instance.

Why

isoduration.parse_duration builds each component amount with Decimal(...). For an exponent past the context's Emax — e.g. P1E1000000D — or a plain digit run longer than 999999, Decimal raises decimal.Overflow, which is not a subclass of the checker's declared isoduration.DurationParsingException. The exception escapes FormatChecker.check uncaught and propagates out of iter_errors/validate:

>>> v = Draft202012Validator({"format": "duration"}, format_checker=FormatChecker())
>>> list(v.iter_errors("P1E1000000D"))
# decimal.Overflow: [<class 'decimal.Overflow'>]  -- uncaught

The boundary is exact: P1E999999D is accepted, P1E1000000D overflows. The same bug is reachable without an exponent via a long digit run ("P" + "9" * 1000000 + "D").

How

decimal.Overflow is an ArithmeticError, so the checker's raises tuple is broadened to (isoduration.DurationParsingException, ArithmeticError) — the same idiom sibling checkers already use (e.g. raises=(idna.IDNAError, UnicodeError), and the pattern proposed in #1526). FormatChecker.check then wraps it into a FormatError and the instance is reported as invalid.

Tests

Added TestFormatChecker.test_it_rejects_durations_that_overflow_decimal (skipped when the optional isoduration dependency is not installed):

  • P1E1000000D and the 1M-digit-run variant both raise FormatError (both crash with decimal.Overflow before the fix).
  • P1Y2M3DT4H5M6S is still accepted.

Full test suite passes (497 tests). Added a CHANGELOG entry.

🤖 Generated with Codebuff

isoduration's parse_duration builds amounts with Decimal, and amounts
past the context's Emax (an exponent like 1E1000000 or a digit run
longer than 999999) raise decimal.Overflow - an ArithmeticError, not
a DurationParsingException. That escaped the format checker uncaught.
Include ArithmeticError in the checker's raises tuple so such strings
are reported as invalid durations like every other malformed instance.

Fixes python-jsonschema#1511

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
@Julian Julian closed this Aug 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

is_duration raises an uncaught decimal.Overflow on a duration with a large exponent

2 participants